package com.lee.boot.head.first.demo.socket.server;

import org.springframework.util.StringUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * @author: Lee
 * @Date: 2019/1/11 16:18
 * @Description:
 */
public class SocketServer {
    private static BufferedReader br = null;
    private static PrintWriter pw = null;
    private static ServerSocket ss;
    private static Socket s;


    public static void main(String[] args) {
        try {
            ss = new ServerSocket(5500);
            ss.setReceiveBufferSize(1000);
            System.out.println("服务器正常启动。。。。");
            int a = 0;
            while (true) {
                s = ss.accept();// 阻塞方法
                System.out.println("连接成功" + s.getRemoteSocketAddress());
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
                pw.println("aaaaaaasasdsadsadsaasdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
                pw.flush();
                String string = br.readLine();
                if (!StringUtils.isEmpty(string)) {
                    System.out.println(++a + " Server读到：" + string);
                }
                s.close();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            pw.close();
            br.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

}
